package controller;

import dao.NoticeDao;
import dao.OrderDao;
import dao.UserDao;
import org.apache.ibatis.session.SqlSession;
import pojo.Notice;
import pojo.Order;
import pojo.User;
import utils.MyBatisUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/**
 * @author HLF
 * @date 2022/6/15 10:38
 */
@WebServlet("/noticeController")
public class NoticeController extends BaseController {
    /**
     * 获取消息列表
     */
    public List<Notice> list() {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        NoticeDao noticeDao = sqlSession.getMapper(NoticeDao.class);

        List<Notice> notices = noticeDao.getAllNotices();
        sqlSession.close();
        return notices;
    }

    /**
     * 跳转到通知消息页面
     */
    protected void index(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<Notice> notices = this.list();
        req.setAttribute("notices", notices);

        req.getRequestDispatcher("/noticeManage.jsp").forward(req, resp);
    }

    /**
     * 添加消息
     */
    protected void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String context = req.getParameter("context");

        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        NoticeDao noticeDao = sqlSession.getMapper(NoticeDao.class);

        noticeDao.insertNotice(new Notice(null, context));

        sqlSession.commit();

        sqlSession.close();
        this.index(req, resp);//重新跳转到通知消息页面
    }

    /**
     * 修改消息
     */
    protected void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String nid = req.getParameter("nid");
        String context = req.getParameter("context");

        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        NoticeDao noticeDao = sqlSession.getMapper(NoticeDao.class);

        noticeDao.updateNotice(new Notice(Integer.valueOf(nid),context));

        sqlSession.commit();
        sqlSession.close();
        this.index(req, resp);//重新跳转到通知消息页面
    }

    /**
     * 根据nid获取消息
     */
    protected void query(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String nid = req.getParameter("nid");
        resp.setContentType("text/html;charset=UTF-8");

        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        NoticeDao noticeDao = sqlSession.getMapper(NoticeDao.class);

        Notice qnotice = noticeDao.getByNid(Integer.valueOf(nid));

        req.setAttribute("qnotice", qnotice);

        this.index(req, resp);
    }

    /**
     * 删除消息
     */
    protected void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String nid = req.getParameter("nid");

        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        NoticeDao noticeDao = sqlSession.getMapper(NoticeDao.class);

        int i = noticeDao.deleteByNid(Integer.valueOf(nid));

        sqlSession.commit();
        sqlSession.close();
        this.index(req, resp);
    }
}
